import 'package:flutter/material.dart';

/// 响应式布局辅助类
class ResponsiveHelper {
  static const double mobileBreakpoint = 480;
  static const double tabletBreakpoint = 768;
  static const double desktopBreakpoint = 1024;
  
  /// 判断设备类型
  static DeviceType getDeviceType(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    
    if (width < mobileBreakpoint) {
      return DeviceType.mobile;
    } else if (width < tabletBreakpoint) {
      return DeviceType.mobileLarge;
    } else if (width < desktopBreakpoint) {
      return DeviceType.tablet;
    } else {
      return DeviceType.desktop;
    }
  }
  
  /// 根据设备类型获取推荐的网格列数
  static int getRecommendedColumns(BuildContext context) {
    final deviceType = getDeviceType(context);
    
    switch (deviceType) {
      case DeviceType.mobile:
        return 2;
      case DeviceType.mobileLarge:
        return 2;
      case DeviceType.tablet:
        return 3;
      case DeviceType.desktop:
        return 4;
    }
  }
  
  /// 根据设备类型获取推荐的字体大小
  static double getRecommendedFontSize(BuildContext context, FontSizeType type) {
    final deviceType = getDeviceType(context);
    
    switch (type) {
      case FontSizeType.title:
        switch (deviceType) {
          case DeviceType.mobile:
            return 16;
          case DeviceType.mobileLarge:
            return 17;
          case DeviceType.tablet:
            return 18;
          case DeviceType.desktop:
            return 20;
        }
      case FontSizeType.body:
        switch (deviceType) {
          case DeviceType.mobile:
            return 14;
          case DeviceType.mobileLarge:
            return 14;
          case DeviceType.tablet:
            return 15;
          case DeviceType.desktop:
            return 16;
        }
      case FontSizeType.caption:
        switch (deviceType) {
          case DeviceType.mobile:
            return 12;
          case DeviceType.mobileLarge:
            return 12;
          case DeviceType.tablet:
            return 13;
          case DeviceType.desktop:
            return 14;
        }
    }
  }
  
  /// 根据设备类型获取推荐的间距
  static double getRecommendedPadding(BuildContext context, PaddingType type) {
    final deviceType = getDeviceType(context);
    
    switch (type) {
      case PaddingType.small:
        return deviceType == DeviceType.mobile ? 8 : 12;
      case PaddingType.medium:
        return deviceType == DeviceType.mobile ? 16 : 20;
      case PaddingType.large:
        return deviceType == DeviceType.mobile ? 24 : 32;
    }
  }
  
  /// 检查是否为紧凑布局（小屏设备）
  static bool isCompactLayout(BuildContext context) {
    return getDeviceType(context) == DeviceType.mobile;
  }
  
  /// 检查是否为宽屏设备
  static bool isWideScreen(BuildContext context) {
    final deviceType = getDeviceType(context);
    return deviceType == DeviceType.tablet || deviceType == DeviceType.desktop;
  }
}

/// 设备类型枚举
enum DeviceType {
  mobile,       // < 480px
  mobileLarge,  // 480-768px
  tablet,       // 768-1024px
  desktop,      // > 1024px
}

/// 字体大小类型
enum FontSizeType {
  title,
  body,
  caption,
}

/// 间距类型
enum PaddingType {
  small,
  medium,
  large,
}

/// 响应式扩展方法
extension ResponsiveExtension on BuildContext {
  /// 获取设备类型
  DeviceType get deviceType => ResponsiveHelper.getDeviceType(this);
  
  /// 是否为移动设备
  bool get isMobile => deviceType == DeviceType.mobile;
  
  /// 是否为平板
  bool get isTablet => deviceType == DeviceType.tablet;
  
  /// 是否为桌面设备
  bool get isDesktop => deviceType == DeviceType.desktop;
  
  /// 是否为宽屏
  bool get isWideScreen => ResponsiveHelper.isWideScreen(this);
  
  /// 获取推荐列数
  int get recommendedColumns => ResponsiveHelper.getRecommendedColumns(this);
} 